﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic;
using LotteryVote.Entity;
using LotteryVote.Logic.Web;
using LotteryVote.Utility;

namespace LotteryVote.Controls
{
    public partial class AddDropWater : CompanyControl
    {
        private bool _displayCompany = true;
        public bool DisplayCompany
        {
            get
            {
                return _displayCompany;
            }
            set
            {
                _displayCompany = value;
            }
        }
        public event EventHandler OnAdded;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnInit(EventArgs e)
        {
            Execute(() =>
            {
                if (!IsPostBack)
                    BindCompays();
            });
            base.OnInit(e);
        }

        void BindCompays()
        {
            ckl_company.DataSource = TodayLotteryCompany.Instance.GetTodayCompany();
            ckl_company.DataTextField = LotteryCompany.NAME;
            ckl_company.DataValueField = LotteryCompany.COMPANYID;
            ckl_company.DataBind();
        }

        protected void btn_AddDropWater_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                double dropValue = double.Parse(txt_dropwater.Text);
                decimal amount = decimal.Parse(txt_amount.Text);
                DropWaterFacade blDropWater = new DropWaterFacade();
                if (DisplayCompany)
                    blDropWater.AddDropWater(txt_num.Text, lv_gameplayway.GamePlayWayId, dropValue, amount, GetCheckedCompany().ToArray());
                else
                    blDropWater.AddAutoDropWater(txt_num.Text, lv_gameplayway.GamePlayWayId, dropValue, amount);
                if (OnAdded != null)
                    OnAdded(this, e);
            });
        }

        private IList<int> GetCheckedCompany()
        {
            List<int> companys = new List<int>();
            foreach (ListItem item in ckl_company.Items)
            {
                if (item.Selected)
                    companys.Add(int.Parse(item.Value));
            }
            return companys;
        }
    }
}